﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using JOATServer.SocietyFramework.Core.XML;
using JOATServer.SocietyFramework.Core;

namespace JOATServer
{
    class Program
    {

        static void Main(string[] args)
        {
            ServerSocietyManager societyManager = new ServerSocietyManager();

            //UIManager
            UIManagerAutomaton uiManagerAutomaton = new UIManagerAutomaton();
            societyManager.AddAutomaton(SocietyInformation.UI_MANAGER_NAME, uiManagerAutomaton);
            MessageRouter.Instance.Subscribe(SocietyInformation.UI_MANAGER_NAME, uiManagerAutomaton);
            uiManagerAutomaton.Start();

            //NewsAutomaton
            NewsAutomaton newsAutomaton = new NewsAutomaton();
            societyManager.AddAutomaton("NewsAutomaton", newsAutomaton);
            MessageRouter.Instance.Subscribe("NewsAutomaton", newsAutomaton);
            newsAutomaton.Start();

            MessageRouter.Instance.StartNetworkMessaging();
            societyManager.StartAutomataTransfer();
        }

        /*
        static private TcpListener tcpListener;
        static private Thread listenThread;
        static private StringBuilder sb;
        static private string fileName = @"C:\Users\Andy\Desktop\aut.xml";
        
        static private void Test()
        {
            tcpListener = new TcpListener(IPAddress.Any, 2203);
            listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();
        }

        static private void ListenForClients()
        {
            tcpListener.Start();
            while (true)
            {
                Console.WriteLine("Waiting for next client...");
                TcpClient client = tcpListener.AcceptTcpClient();

                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        static private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            //TestThing(clientStream, fileName);
            //StreamToFile(clientStream, fileName);
            //ReadXML(clientStream);

            tcpClient.Close();
        }

        public static void TestThing(Stream input, string fileName)
        {
            using(FileStream fs = new FileStream(fileName, FileMode.Create)){
                XMLAutomaton xmlAutomaton = new XMLAutomaton();
                xmlAutomaton.DeserializeFromStream(input);
                xmlAutomaton.SerializeToStream(fs);
            }
        }

        public static void StreamToFile(Stream input, string fileName)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fileStream.Write(buffer, 0, len);
                }
            }

            Console.WriteLine("Successfully written aut.xml.");
        }

        static void ReadXML(NetworkStream clientStream)
        {
            sb = new StringBuilder();
            using (XmlReader reader = XmlReader.Create(clientStream))
            {
                while (reader.Read())
                {
                        sb.Append("<" + reader.Name + ">");
                }
            }

            File.WriteAllText(fileName, sb.ToString());

            Console.WriteLine("Successfully written aut.xml.");
        }
        */
    }
}
